/*
 * @Des: 
 * @Author: jin_yc
 * @Date: 2020-01-06 15:46:12
 * @LastEditor: jin_yc
 * @LastEditTime: 2021-12-30 19:39:40
 */
import './subapp/public-path'
import Vue from 'vue'
import App from './App.vue'
import store from './store'
import { LOADING_INIT_DATA, LOADING_USER_DATA } from '@/store/Action/actionTypes'
import { CLEAN_WEBSOCKET_POOL } from '@/store/Mutation/mutationTypes'
import "@/assets/css/app.scss" //app公共样式

import { setTs } from '@/utils/request/sign'
import { tsDvalue, subApp, appName, appPathPrefix } from './config'
import Api from '@/config/api'

//在install前注入环境变量
Object.assign(Vue.prototype, Api)
Vue.prototype.$appBasePath = subApp ? '/' + appPathPrefix + appName : ''
Vue.prototype.$appName = appName
Vue.prototype.$subApp = subApp

Vue.config.productionTip = false

let instance




// 确保独立运行
window.__POWERED_BY_QIANKUN__ || render()



//添加.$mount(container ? container.querySelector('#app') : '#app');还有对应的各种props
//排除根DOM id相同
async function render (props = {}) {
  const { container } = props

  let install = require('@/utils/install').default
  let router = require('@/permission').default
  Vue.use(install)

  await setTs()
  window.setInterval(() => {
    setTs()
  }, tsDvalue)
  await store.dispatch(LOADING_INIT_DATA)
  await store.dispatch(LOADING_USER_DATA)
  instance = new Vue({
    router,
    store,
    render: h => h(App)
  }).$mount(container ? container.querySelector('#fks-app') : '#fks-app')

}

export async function bootstrap (props = {}) {
  // 父应用传递的值 挂载vue原型上
  Vue.prototype.$fksMainData = { ...props.data }
  Vue.prototype.$appName = props.appName || Vue.prototype.$appName
  Vue.prototype.$appBasePath = props.appBasePath || '/' + appPathPrefix + (Vue.prototype.$appName || appName)

  // 父应用传递的 方法 挂载原型上
  Vue.prototype.$fksMainFunc = props.func

  // 父应用传递的 store 挂载原型上，添加响应
  Vue.prototype.$fksMainStore = Vue.observable(props.store)

  Vue.prototype.$InQianKun = true

  Vue.prototype.$subApp = true
}

export async function mount (props) {
  await render(props)
}

export async function unmount () {
  store.commit(CLEAN_WEBSOCKET_POOL)
  if (instance) {
    instance.$el.innerHTML = ''
    instance.$destroy()
    instance = null
  }
}


